import _ from 'lodash'
import { getGlobal } from '@jsjn/utils'
export {
    getGlobal,
    global,
    filterObj,
    assignOwnProp,
    emptyObj,
    findParentByPId,
    findTargetById,
    toThousands,
    restrictDecimals,
    getAllLeaf,
    size2Rem,
    getUrlParams
} from '@jsjn/utils'

getGlobal()

/**
 *  禁用树节点类型不为机构的节点
 * @param tree
 */
export const disabledTreeNodes = (tree: any[]) => {
    tree.forEach((item) => {
        if (item.type === 'QH' || item.type === 'QZ' || item.type === 'QW') {
            item.disabled = true
        }
        if (item.children && item.children.length > 0) {
            disabledTreeNodes(item.children)
        }
    })
}

/**
 * 扁平化树节点
 * @param tree
 * @returns type为机构的节点
 */
export const treeToOrgArray = (tree) => {
    let stack = _.cloneDeep(tree),
        result: any = []
    while (stack.length !== 0) {
        let pop = stack.pop()
        result.push({
            id: pop.id,
            name: pop.name,
            type: pop.type
        })
        let children = pop.children
        if (children) {
            for (let i = children.length - 1; i >= 0; i--) {
                stack.push(children[i])
            }
        }
    }
    return result.filter((item) => {
        return item.type !== 'QH' && item.type !== 'QZ' && item.type !== 'QW'
    })
}
